﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace orderCreater.ben
{
    public partial class frmPhone : Form
    {
        public class num_area
        {
            public string province;
            public string area;
            public string url;
        }
        public OrderCreaterModel OCM = new OrderCreaterModel(ConfigurationManager.AppSettings["ConnStr-addr"]);
        public List<num_area> area_url = new List<num_area>();
        public List<string> area_phones = new List<string>();
        public int tick_index = 0, area_index=0;
        public frmPhone()
        {
            InitializeComponent();
            webBrowser1.Navigate("http://www.hiphop8.com/all.html");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (var i = 0; i < 10000; i++)
            {
                area_phones.Add((i < 10 ? "000" + i.ToString() : (i < 100 ? "00" + i.ToString() : (i < 1000 ? "0" + i.ToString() : i.ToString()))));
                listBox1.Items.Add((i<10?"000"+i.ToString():(i < 100 ? "00" + i.ToString():(i < 1000 ? "0" + i.ToString() :i.ToString()))));
                File.WriteAllLines(@"D:\doc\abc.txt", area_phones);
            }
            
            //foreach (HtmlElement ElementBody in webBrowser1.Document.GetElementsByTagName("div"))
            //{
            //    if (ElementBody.GetAttribute("className") == "all")
            //    {
            //        foreach (HtmlElement li in ElementBody.GetElementsByTagName("li"))
            //        {
            //            foreach (HtmlElement a in li.GetElementsByTagName("a"))
            //            {
            //                num_area item = new num_area();
            //                item.url = a.GetAttribute("href");
            //                item.area = a.InnerText;

            //                var mprovince = item.url.Substring(0,item.url.LastIndexOf("/"));
            //                foreach (HtmlElement dd in ElementBody.GetElementsByTagName("a"))
            //                {
            //                    if (dd.GetAttribute("href") == mprovince)
            //                    {
            //                        item.province = dd.InnerText;
            //                        break;
            //                    }
            //                }
            //                area_url.Add(item);
            //            }
            //        }
            //        timer1.Enabled = true;
            //        break;
            //    }
            //}
        }



        private void timer1_Tick(object sender, EventArgs e)
        {
            if (area_index < area_url.Count)
            {
                switch (tick_index % 3)
                {
                    case 0:
                        listBox1.Items.Add(area_url[area_index].url);
                        webBrowser2.Navigate(area_url[area_index].url);
                        webBrowser2.ScriptErrorsSuppressed = true;
                        break;
                    case 1:
                        area_phones.Clear();
                        foreach (HtmlElement li in webBrowser2.Document.GetElementsByTagName("li"))
                        {
                            foreach (HtmlElement a in li.GetElementsByTagName("a"))
                            {
                                //D:\doc
                                area_phones.Add(area_url[area_index].province + ","+area_url[area_index].area + "," + a.InnerText);
                            }
                        }
                        break;
                    case 2:
                        File.WriteAllLines(@"D:\doc\"+ area_url[area_index].province + "_" + area_url[area_index].area+".txt", area_phones);
                        area_index++;
                        break;
                }
            }
            else {
                listBox1.Items.Add("完成");
                timer1.Enabled = false;
            }
            tick_index++;
        }
    }
}
